﻿Imports DTO
Imports BUS
Public Class frmBaoCaoDoanhSo
    Private _tenNhanVien As String
    Public Property TenNhanVien() As String
        Get
            Return _tenNhanVien

        End Get
        Set(ByVal value As String)
            _tenNhanVien = value
        End Set
    End Property
    Private Sub frmBaoCaoDoanhSo_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

       
    End Sub

    Public Sub KhoiTaoRePort(ByVal o As Object)
        CrystalReportViewer1.ReportSource = o
    End Sub

    Private Sub LoadReport()
        Dim ltkBus As New LoaiTietKiemBUS()
        Dim stkBus As New SoTietKiemBUS()
        Dim pgtBus As New PhieuGoiTienBUS()
        Dim prtBus As New PhieuRutTienBUS()
        Dim dt As New DataTable()

        Dim lstLoaiTietKiem As New List(Of LoaiTietKiemDTO)
        lstLoaiTietKiem = LoaiTietKiemBUS.LayLoaiTietKiem()

        Dim dtTongThuSoTietKiem As New DataTable()
        dtTongThuSoTietKiem = stkBus.TongThu(dtpNgay.Value)

        Dim dtTongThuPheiuGoiTien As New DataTable()
        dtTongThuPheiuGoiTien = pgtBus.TongThu(dtpNgay.Value)

        Dim dtTongChiPhieuRutTien As New DataTable()
        dtTongChiPhieuRutTien = prtBus.TongChi(dtpNgay.Value)

        'DataGridView1.DataSource = dtTongThuPheiuGoiTien
        'dt.Columns.Add("MaLoaiTietKiem")
        dt.Columns.Add("TenLoaiTietKiem")
        dt.Columns.Add("TongThu")
        dt.Columns.Add("TongChi")
        dt.Columns.Add("ChenhLech")
        For i As Integer = 0 To lstLoaiTietKiem.Count - 2
            Dim _tongthu As Long
            Dim _tongthu1 As Long
            Dim _tongthu2 As Long
            Dim _tongchi As Long
            Dim _chenhlech As Long

            For k As Integer = 0 To dtTongThuSoTietKiem.Rows.Count - 1
                If lstLoaiTietKiem(i).MaLoaiTietKiem = Convert.ToInt32(dtTongThuSoTietKiem.Rows(k)("MaLoaiTietKiem")) Then
                    _tongthu = Convert.ToInt64(dtTongThuSoTietKiem.Rows(k)("TongThu"))
                    Exit For
                Else
                    _tongthu = 0
                End If

            Next
            For j As Integer = 0 To dtTongThuPheiuGoiTien.Rows.Count - 1
                If lstLoaiTietKiem(i).MaLoaiTietKiem = Convert.ToInt32(dtTongThuPheiuGoiTien.Rows(j)("MaLoaiTietKiem")) Then
                    _tongthu1 = Convert.ToInt64(dtTongThuPheiuGoiTien.Rows(j)("TongThu"))
                    Exit For
                Else
                    _tongthu1 = 0
                End If

            Next

            For j As Integer = 0 To dtTongChiPhieuRutTien.Rows.Count - 1
                If lstLoaiTietKiem(i).MaLoaiTietKiem = Convert.ToInt32(dtTongChiPhieuRutTien.Rows(j)("MaLoaiTietKiem")) Then
                    _tongchi = Convert.ToInt64(dtTongChiPhieuRutTien.Rows(j)("TongChi"))
                    Exit For
                Else
                    _tongchi = 0
                End If
            Next
            _tongthu2 = _tongthu + _tongthu1
            _chenhlech = _tongthu2 - _tongchi
            dt.Rows.Add(lstLoaiTietKiem(i).TenLoaiTietKiem, _tongthu2, _tongchi, _chenhlech)
        Next
        'DataGridView1.DataSource = dt

        Dim rpt As New CrystalReportBaoCaoDoanhSo()
        rpt.SetDataSource(dt)
        rpt.SetParameterValue("@para1", dtpNgay.Value.Date())
        rpt.SetParameterValue("NguoiLapBaoCao", _tenNhanVien.ToString())
        CrystalReportViewer1.ReportSource = rpt
    End Sub
    Private Sub btnBaoCao_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBaoCao.Click
        LoadReport()
    End Sub
End Class